import Mock from 'mockjs';

Mock.setup({
  timeout: 400,
});

const {roleList} = Mock.mock({
  'roleList|2-10': [
    {
      'roleId|+1': 1,
      roleName: '@ctitle(4,6)'
    },
  ]
});

Mock.mock(/Role\/List/, 'get', (options) => {
  return roleList;
});

Mock.mock(/Role\/Add/, 'post', (options) => {
  const {body} = options;
  const {roleName} = JSON.parse(body);

  const obj = {
    roleName,
    roleId: roleList.length + 1,
  }

  roleList.push(obj);

  return {
    success: true,
    message: '添加角色成功',
  }
});

const {guestList} = Mock.mock({
  'guestList|10': [
    {
      'guestId|+1': 1,
      guestName: '@ctitle(2,4)',
      'identityId|100000000000000000-999999999999999999': 1,
      'phone|13000000000-13099999999': 1,
      'roomId|+1': 101,
      room: {
        roomType: {
          roomTypeName: '@ctitle(3)',
          'roomTypeId|1-3': 1,
        },
      },
      resideDate: '@datetime(yyyy/MM/dd HH:mm:ss)',
      leaveDate: '@datetime(yyyy/MM/dd HH:mm:ss)',
      'deposit|100-999': 1,
      'totalMoney|1000-9999': 1,
      resideState: {
        'resideStateId|1-2': 1,
        resideStateName: '@ctitle(3)',
      },
      'guestNum|1-3': 1,
    },
  ]
});

const {guestRes} = Mock.mock({
  guestRes: {
    data: guestList,
    'count|10-100': 1,
  },
});

Mock.mock(/GuestRecord\/List/, 'get', (options) => {
  console.log('options:');
  console.log(options);

  const {url} = options;
  const params = url.split('?')[1].split('&');
  let pageIndex = 1;
  let pageSize = 10;

  params.forEach(equal => {
    const arr = equal.split('=');

    if(arr[0] === 'pageIndex'){
      //当前页
      pageIndex = arr[1];
    }else if(arr[0] === 'pageSize'){
      //每页显示条数
      pageSize = arr[1];
    }
  });

  console.log('pageIndex:');
  console.log(pageIndex);

  return {
    ...guestRes,
    pageIndex,
    pageSize,
  }
});

const resideStateList = [
  {
    resideStateId: 1,
    resideStateName: '未结账',
  },
  {
    resideStateId: 2,
    resideStateName: '已结账',
  },
];

Mock.mock(/ResideState\/List/, 'get', (options) => {
  return resideStateList;
});

const {totalMoney} = Mock.mock({
  'totalMoney|100-9999': 1,
});

Mock.mock(/GuestRecord\/Checkout/, 'post', (options) => {
  return {
    totalMoney,
  }
});

const {totalTypePriceList} = Mock.mock({
  'totalTypePriceList|9': [
    {
      'totalMoney|0-1000': 1,
      roomTypeName: '@ctitle(3,4)'
    },
  ]
});

Mock.mock(/RoomType\/TotalTypePrice/, 'get', (options) => {
  return totalTypePriceList;
});